package com.bdqn.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Slf4j
@Component
@Aspect
public class UserLogAspect {
    @Pointcut("execution(* com.bdqn.mapper.*.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void before(JoinPoint jp) {

        log.info("前置增强...调用{}方法", jp.getTarget().getClass());
    }

    @AfterReturning(pointcut ="pointCut()",returning = "result")
    public void afterReturning(JoinPoint jp,Object result) {
        log.info("后置增强...调用{}方法,返回值是{}",  jp.getTarget().getClass(),result);
    }

    @AfterThrowing(pointcut = "pointCut()",throwing = "e")
    public void afterThrowing(JoinPoint jp,Exception e) {
        log.error("{}方法出现异常:{}", jp.getSignature().getName(),e.getMessage());
    }

    @After("pointCut()")
    public void after(JoinPoint jp) {
        log.info("最终增强...{}方法",jp.getTarget());
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        log.info("环绕增强前...");
        Object result = pjp.proceed();
        log.info("环绕增强后...");
        Object target = pjp.getTarget();
        //1.获取目标对象
        log.info("获取目标对象:{}",target);
        //2.目标类
        String className = pjp.getTarget().getClass().getName();
        log.info("获取目标类:{}",className);
        //3.目标方法
        String name = pjp.getSignature().getName();
        log.info("获取目标方法:{}",name);
        //4.获取目标方法的参数
        Object[] args = pjp.getArgs();
        log.info("获取目标方法参数:{}", Arrays.toString(args));

        return result;
    }
}
